package com.fruit.service.order.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.fruit.constants.OrderConstant;
import com.fruit.pojo.order.DSHOrder;
import com.fruit.pojo.order.Order;
import com.fruit.pojo.pay.PayInfo;
import com.fruit.service.order.OrderHandel;
import com.fruit.service.order.OrderManager;
import com.fruit.service.redis.JedisUtil;
import com.fruit.util.CustomUtil;
import com.fruit.util.ResultInfo;
import com.fruit.util.ResultUtil;

import lombok.extern.slf4j.Slf4j;


/**
 * 
 * 订单处理逻辑类（无事物）
 *
 * @author shibeilei
 * @version 2017.12.01 10:12
 *
 */
@Slf4j
@Service
public class OrderHandelNothing implements OrderHandel {
	
	@Autowired
	private OrderManager orderService;

	@Autowired
	private OrderLogService orderLogService;

	@Autowired
	private JedisUtil jedisService;

	// private SmsCodeManager smsCodeService;

	@Override
	public Map<String, Object> autoCancelOrder(String tradeId, DSHOrder dshOrder, int times) {
		Map<String, Object> resMap = new HashMap<String, Object>();
		log.info("[{}]-【取消订单】-【处理开始】", tradeId);
		Order order = new Order();
		try {
			for (int i = 1; i <= times; i++) {
				log.info("[{}]-【取消订单】-【处理开始】-【第{}次】", tradeId, i);
				resMap = orderService.updateAutoCancelOrder(tradeId, order, dshOrder);
				ResultInfo ri = (ResultInfo) resMap.get("result");
				if (!StringUtils.equals("again_start", ri.getCode())) {
					break;
				}
				if (i < times) {
					CustomUtil.sleep(30, 50);
				}
			}
			return resMap;
			// 判断并插入日志
		} catch (Exception e) {
			log.error("[{}]-【取消订单】-【异常】-【{}：{}】----{}", tradeId, "cancelOrderException", "取消订单异常!",
					CustomUtil.getStackTraceAsString(e));
			resMap.put("result", ResultUtil.initResult(ResultInfo.ERROR, "cancelOrderException", "取消订单异常!"));
			resMap.put("serviceState", OrderConstant.ServiceState.cancelUpdateOrder.getValue());
			return resMap;
		} finally {
			ResultInfo ri = (ResultInfo) resMap.get("result");
			if (ResultInfo.ERROR.equals(ri.getState())) {
				log.error("[{}]-【取消订单】-【异常】-【{}：{}】", tradeId, ri.getCode(), ri.getMessage());
				if (!StringUtils.isEmpty(order.getCode())) {
					// 插入错误日志
					String serviceState = (String) resMap.get("serviceState");
					orderLogService.newSaveExceptionOrder(tradeId, "OrderHandelNothing", serviceState, order.getCode(),
							order.getUserId(), ri.getCode(), ri.getMessage());
				}
			} else {
				log.info("[{}]-【取消订单】-【成功】", tradeId);
			}
			log.info("[{}]-【取消订单】-【结束】", tradeId);
		}

	}

	@Override
	public void runSaveAutoCancelOrder(List<Order> orderList, PayInfo pi) {
		new Thread(new Runnable() {
			@Override
			public void run() {

				// 调用自动退订
				for (int i = 0; i < orderList.size(); i++) {
					Order order = orderList.get(i);
					orderService.newSaveAutoCancelOrder(order.getId());
				}

				// 发送短信给用户
				// String phones = "['" + pi.getPhone() +"']";
				// String params = "['" +
				// pi.getCompanyName()+"','"+pi.getRemittanceCode() +"']";
				// smsCodeService.sendSmsNotice("user_pay", phones, params);
			}
		}).start();

	}

}
